package com.bac.rentmap.web.controller.osm;

import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bac.rentmap.manager.BuildingManager;
import com.bac.rentmap.manager.StreetManager;
import com.bac.rentmap.model.Building;
import com.bac.rentmap.model.Street;

import java.util.List;
import java.util.ArrayList;

/**
 *
 * @author Ivan Holub
 */
public class OsmTextController extends AbstractController {

    private String viewName;

    private BuildingManager buildingManager;

    private StreetManager streetManager;

    public void setStreetManager(StreetManager streetManager) {
        this.streetManager = streetManager;
    }

    public void setViewName(String viewName) {
        this.viewName = viewName;
    }

    public void setBuildingManager(BuildingManager buildingManager) {
        this.buildingManager = buildingManager;
    }

    protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
//        List<Building> buildings = buildingManager.getBuildings(100);
//        List<MarkerItem> markers = new ArrayList<MarkerItem>();
//        for (Building b : buildings) {
//            MarkerItem m = new MarkerItem();
//            m.setLat(b.getLat());
//            m.setLon(b.getLon());
//            m.setIcon("http://www.openlayers.org/dev/img/marker.png");
//            m.setTitle(b.getStreet() + " " + b.getHousenumber());
//            m.setDescription("row1<br/>row2");
//            markers.add(m);
//        }

        List<Street> streets = streetManager.getStreets(100);
        List<MarkerItem> markers = new ArrayList<MarkerItem>();
        for (Street b : streets) {
            MarkerItem m = new MarkerItem();
            m.setLat(b.getLat());
            m.setLon(b.getLon());
            m.setIcon("http://www.openlayers.org/dev/img/marker.png");
            m.setTitle(b.getName());
            m.setDescription("row1<br/>row2");
            markers.add(m);
        }

        ModelAndView mav = new ModelAndView(viewName);
        mav.addObject("markers", markers);
        mav.addObject("tab", "\t");
        return mav;
    }
}
